package com.cx.base.day15_socket.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Socket2_TCP_ServerSocket {
    public static byte[] bs = new byte[1024];
    private static List<Socket> socketList = new ArrayList<>();

    public static void main(String[] args) {
        System.out.println("中文");
        try {
            System.out.println("wait conn");
            //阻塞1
            ServerSocket serverSocket = new ServerSocket(8080);
            while (true) {
                //阻塞2
                Socket socket = serverSocket.accept();
                if (socket != null) {
                    socketList.add(socket);
                    System.out.println("connect success");
                }
                for (Socket socketE : socketList) {
                    int len = socketE.getInputStream().read(bs);
                    System.out.println(new String(bs, 0, len));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}
